package com.zytool.easyrestful.core.utils;

import com.zytool.easyrestful.core.annotation.Condition;
import com.zytool.easyrestful.core.convert.ConvertorHolder;

import javax.persistence.Column;
import java.beans.Transient;
import java.lang.reflect.Field;

public class FieldUtil {
    private FieldUtil() {
    }

    public static String getColumnName(Field field) {
        return ConvertorHolder.instance().getConvertor().field2Column(field);
    }

    public static boolean conditionable(Field field) {
        return field.getAnnotation(Condition.class) != null;
    }

    public static boolean selectable(Field field) {
        return field.getAnnotation(Transient.class) != null;
    }

    public static boolean insertable(Field field) {
        if (field.getAnnotation(Transient.class) != null) {
            return false;
        }
        Column column = field.getAnnotation(Column.class);
        return column == null || column.insertable();
    }

    public static boolean updateable(Field field) {
        if (field.getAnnotation(Transient.class) != null) {
            return false;
        }
        Column column = field.getAnnotation(Column.class);
        return column == null || column.insertable();
    }
}